package com.lungle.warmdiary.app.view.active.fragment.status;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.lungle.warmdiary.app.AppContext;
import com.lungle.warmdiary.app.api.remote.ApiFactory;
import com.lungle.warmdiary.app.base.BaseListFragment;
import com.lungle.warmdiary.app.base.ListBaseAdapter;
import com.lungle.warmdiary.app.model.Result;
import com.lungle.warmdiary.app.model.event.ActiveStatueRefreshEvent;
import com.lungle.warmdiary.app.model.event.MediaChangeEvent;
import com.lungle.warmdiary.app.model.response.Active;
import com.lungle.warmdiary.app.model.response.ActiveList;
import com.lungle.warmdiary.app.ui.empty.EmptyLayout;
import com.lungle.warmdiary.app.utils.LogUtils;
import com.lungle.warmdiary.app.utils.UIHelper;
import com.lungle.warmdiary.app.view.active.ActiveTab;
import com.lungle.warmdiary.app.view.active.adapter.ActiveAdapter;
import com.umeng.analytics.MobclickAgent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.InputStream;
import java.io.Serializable;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;


public abstract class ActiveStatusFragment extends BaseListFragment<Active> {
    private static final String ACTIVE_HOT_SCREEN = "active host list screen";
    String mMediaId;
//    private MediaReceiver mMediaReceiver;

//    @Override
//    protected boolean requestDataIfViewCreated() {
//        return false;
//    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        isActive = true;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//        mMediaReceiver = new MediaReceiver();
//        IntentFilter filter = new IntentFilter();
//        filter.addAction(AppConstant.INTENT_ACTION_Media_change);
//        getActivity().registerReceiver(mMediaReceiver, filter);


        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onResume() {
        isActive = true;
        try {
            if (!AppContext.hasMediaAccount()) {
                mErrorLayout.setErrorType(EmptyLayout.NO_ADD_MEDIA);
            } else {
                mErrorLayout.setNoDataContent(String.format("%s%s%s", "您暂无", getResources().getString(ActiveTab.getTabByIdx(getStatus()).getTitle()), "活动，请稍后查看"));
            }
        } catch (Throwable e) {
            LogUtils.printLogD(e.getMessage());
        }
        super.onResume();

        MobclickAgent.onResume(getActivity());

        if (onResumeRefresh) {
            LogUtils.printLogD("ActiveStatusFragment onResumeRefresh");
            refresh();
            onResumeRefresh = false;

        }

    }


    @Override
    public void onPause() {
        super.onPause();
//        MobclickAgent.onPageEnd(ACTIVE_HOT_SCREEN);
        MobclickAgent.onPause(getActivity());

    }


    @Override
    protected ListBaseAdapter getListAdapter() {
        ActiveAdapter activeAdapter = new ActiveAdapter(ActiveAdapter.TYPE_NOSTATUS);
        return activeAdapter;
    }


    @Override
    protected ActiveList parseList(InputStream is) throws Exception {
        return ActiveList.parse(is);
    }


    @Override
    protected ActiveList readList(Serializable seri) {
        return (ActiveList) seri;
    }

    @Override
    protected void requestData(boolean refresh) {
        sendRequestData();
    }

    @Override
    protected void sendRequestData() {
        LogUtils.printLogD("ActiveStatusFragment sendRequestData" + getStatus());

        ApiFactory.getUserApi().getMyActiveList1(mMediaId, getStatus(), mCurrentPage * getPageSize(), getPageSize()).compose(this.<Result<?>>bindToLifecycle()).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(mSubscriber);
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Active item = mAdapter.getItem(position);
        if (item != null) {
            UIHelper.showActiveDetail(getActivity(), item.getId(), item.getMediaid());
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);


//        if (mMediaReceiver != null && getActivity() != null) {
//            getActivity().unregisterReceiver(mMediaReceiver);
//        }

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();

    }


    abstract int getStatus();

    public void refresh() {
//        if (mState == STATE_REFRESH || mState == STATE_LOADMORE) {
//            return;
//        }
//        mListView.setSelection(0);
//        mCurrentPage = 0;
//        mState = STATE_NONE;
//        mErrorLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
        sendRequestData();

    }


    boolean onResumeRefresh = false;

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onStatueChanged(ActiveStatueRefreshEvent event) {
        onResumeRefresh = true;
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMediaChanged(MediaChangeEvent event) {
        mMediaId = event.getMediaAccount().getId();
        onResumeRefresh = true;
    }

}
